package com.jswlw.controller.admin.biz;

import com.github.pagehelper.Page;
import com.jswlw.controller.BaseController;
import com.jswlw.entity.biz.FeedBack;
import com.jswlw.service.biz.FeedBackService;
import com.jswlw.web.manager.SessionAdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;

@Controller
@RequestMapping("/admin/biz/feedBack")
public class AdminFeedBackController extends BaseController {

    @Autowired
    FeedBackService service;

    @RequestMapping("/index")
    public String index(Model model) {
        return "biz/feedBack/index";
    }

    /**
     * 广告列表
     *
     * @param
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestBody FeedBack feedBack) {
        if (Objects.nonNull(SessionAdminManager.getCurrentUser().getDistributor())) {
            feedBack.setDistributorIds(SessionAdminManager.getCurrentUser().getDistributor().getChildrenIds());
        }
        Page<FeedBack> page = service.queryPage(feedBack, feedBack.getOffset(), feedBack.getLimit());
        return pageResult(page.getResult(), page.getTotal());
    }

    /**
     * 编辑广告
     *
     * @param id
     * @return
     */
    @RequestMapping("/view")
    public ModelAndView view(Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("info", service.find4Id(id));
        modelAndView.setViewName("biz/feedBack/view");
        return modelAndView;
    }

    /**
     * 保存广告
     *
     * @param feedBack
     * @param
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(@RequestBody FeedBack feedBack) {
        try {
            if (feedBack.getId() == null) {
                service.add(feedBack);
            } else {
                service.update(feedBack);
            }
            return this.successResult(null);
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("保存失败" + e.getMessage());
        }
    }

    /**
     * 删除广告
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ModelMap delete(Model model, Integer id) {
        try {
            service.delete(id);
            return this.successResult("删除成功");
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("删除失败" + e.getMessage());
        }
    }

    @RequestMapping("/chgStatus")
    @ResponseBody
    public ModelMap chgStatus(Integer id,Integer status) {
        try {
            FeedBack repairInfo=service.find4Id(id);
            repairInfo.setStatus(status);
            service.update(repairInfo);
            return this.successResult("处理成功");
        } catch (Exception e) {
            logger.error("处理失败", e);
            return this.failResult("处理失败" + e.getMessage());
        }
    }
}
